package com.ticket.sass.admin.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ticket.sass.common.entity.tenant.CommodityEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 卖品 Mapper 接口
 * </p>
 *
 * @author ywb
 * @since 2024-03-06
 */
public interface CommodityMapper extends BaseMapper<CommodityEntity> {
    @Select("""
        SELECT  
            commodity.*,
            category.name AS category_name
        FROM  
           commodity_categories category
        RIGHT JOIN  
           commodities commodity
        ON  
        	commodity.category_id = category.id
        ${ew.customSqlSegment}
        """)
    IPage<CommodityEntity> queryPage(Page<CommodityEntity> page, @Param(Constants.WRAPPER) QueryWrapper<CommodityEntity> queryWrapper);
    
    @Select("""
        SELECT  
            commodity.*,
            category.name AS category_name
        FROM  
           commodity_categories category
        RIGHT JOIN  
           commodities commodity
        ON  
        	commodity.category_id = category.id
        ${ew.customSqlSegment}
        """)
    List<CommodityEntity> queryList(@Param(Constants.WRAPPER) QueryWrapper<CommodityEntity> queryWrapper);
    
    @Select("SELECT commodities.id, commodities.name " +
        "FROM commodities commodities " +
        "INNER JOIN coupon_commodity_maps map ON commodities.id = map.commodity_id " +
        "WHERE map.coupon_id = #{couponId} " +
        "AND map.deleted_at IS NULL " +
        "AND commodities.deleted_at IS NULL")
    List<CommodityEntity> queryCommoditiesByCouponId(@Param("couponId") Long couponId);
}
